import { locale } from 'core-js'
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect:'/user/cart',
    children:[
      {
        path:'cart',
        name:'cart',
        component:()=>import('../components/user/Cart.vue')
      },
      {
        path:'unbind',
        name:'unbind',
        component:()=>import('../components/user/Unbind.vue')
      }
    ],
    //路由独享守卫 
    // beforeEnter:(to,from,next)=>{
    //   let token = localStorage.getItem('x-auth-token')
    //   if(token){
    //     next()
    //   }else{
    //     alert("请先登录")
    //   }
    // }
  },

  {
    path: '/order',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },
 { path:'/*',
  component:()=>import('../components/Error.vue')
}
]
/* 路由的模式 历史 */
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/* 解决连续点击报错. */
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};
//全局路由守卫   如果有重定向，那就要重定向
// router.beforeEach((to,from,next)=>{
//   let token = localStorage.getItem('x-auth-token')
//   if(to.path=='/user/cart'){ 
   
//     if(token){
//       console.log(111)
//       next()
//     }else{
//       alert('请先登录')
//     }
//     return 
//   }
//   next()
// })
export default router
